//
//  GLTollStationListViewController.m
//  Runone
//
//  Created by ekkaLyn on 2017/4/27.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "GLTollStationListViewController.h"
#import "MJExtension.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "RequestGinsengModel.h"
#import "CurrentEvent.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "SVProgressHUD.h"
#import "ParModel.h"
#import "RightCodeTool.h"
#import "TollEvent.h"
#import "AppConstants.h"
#import "AppLocalManager.h"
#import "EventBaseCell.h"
#import "CurrentContructDetailViewController.h"
#import "CurrentIncidentDetailViewController.h"
#import "HistoryInidentDetailController.h"
#import "HistoryConstructDetailController.h"
#import "TollStationEventDetailController.h"
#import "ROTitleDispalyHeader.h"
#import "RightCodeTool.h"

#define PageSize  10

@interface GLTollStationListViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;//保存当前的页数
@end

#pragma  -----------事件管理界面   收费事件


static NSString *cellID = @"historyCell";
@implementation GLTollStationListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kTableViewCellTextColor;
    _page = 0;
    self.dataArray = [[NSMutableArray alloc] init];
    [self fetchNewServer];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.contentInset = UIEdgeInsetsMake(1, 0, 50, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    [self.tableView registerNib:[UINib nibWithNibName:@"EventBaseCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    RegisterNotify(GLStationEventReportNotification, @selector(requestAgain));

}
- (void) requestAgain {
    [self fetchNewServer ];
}
-(void)dealloc {
    RemoveNofify
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [self fetchNewServer];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    self.tabBarController.tabBar.hidden = YES;
}


-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
   // [self fetchNewServer];
}


#pragma mark - 下拉
-(void)loadNewData {
    
    [self fetchNewServer];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

-(void)loadMoreData {
    [self fetchMoreServer];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

//加载新数据
- (void)fetchNewServer {
    [SVProgressHUD dismiss];
    self.page = 0;
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getAllModel=[[RequestGinsengModel alloc] init];
    getAllModel.URL         = GetEventDataURL;
    getAllModel.DataValue   = [self DataValueEventUId:nil pageString:@"1" andSizeString:@"10"];
    getAllModel.UpdatedTime = @"";
    getAllModel.SystemCode  = @"GuangleHighway";
    getAllModel.MethodName  = @"GetAllTollStationEventByPage";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel) {
        
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            _dataArray = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [self.tableView reloadData];
            
        }
    };
    [requestHelper RequestGinsengModel:getAllModel andPushController:self];
}

//加载更多数据
- (void)fetchMoreServer {
    _page  += 1;
    NSString *pageStr  = [NSString stringWithFormat:@"%ld",(long)_page];
    NSString *pageSize = [NSString stringWithFormat:@"%d",PageSize];
    
    RequestHelper *requestHelper   = [[RequestHelper alloc] init];
    RequestGinsengModel *getByPage = [[RequestGinsengModel alloc] init];
    getByPage.URL         = GetEventDataURL;
    CurrentEvent  *lasetModel       = (CurrentEvent *)[self.dataArray lastObject];
    getByPage.DataValue   = [self DataValueEventUId:lasetModel.EventUID pageString:pageStr andSizeString:pageSize];
    getByPage.UpdatedTime = @"";
    getByPage.SystemCode  = @"GuangleHighway";
    getByPage.MethodName  = @"GetAllTollStationEventByPage";;
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        //        NSLog(@"%@",requestOutginseng.DataValue);
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            
            NSArray *tempArray = [TollEvent mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];;
            
            if (tempArray.count == 0) {
                self.page -= 1;
                [SVProgressHUD showSuccessWithStatus:@"没有更多的数据了"];
            }
            
            if (tempArray.count > 1) {
                [self.dataArray addObjectsFromArray:tempArray];
                [self.tableView reloadData];
            }
        }else {
            self.page -= 1;
        }
    };
    [requestHelper RequestGinsengModel:getByPage andPushController:self];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EventBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.backgroundColor = kViewBackGroundColor;
    TollEvent *model = self.dataArray[indexPath.row];
    
    cell.tollData = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TollStationEventDetailController *vc = [[TollStationEventDetailController alloc] init];
    vc.model = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 拼接参数
-(NSString *)DataValueEventUId:(NSString *)eventUID pageString:(NSString *)pageStr andSizeString:(NSString *)sizeStr {
    NSString *valueStr =[NSString stringWithFormat:                                           @"[{\"key\":\"LastEventUID\",\"value\":\"%@\"},                                     {\"key\":\"PageIndex\",\"value\":\"%@\"},                               {\"key\":\"PageSize\",\"value\":\"%@\"}]",eventUID,pageStr,sizeStr];
    
    return valueStr;
}
@end
